package com.zx.map.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import com.zx.map.dao.HistoryPayCardInfoDao;
import com.zx.map.dao.model.HistoryPayCardInfo;
import com.zx.map.dao.model.HistoryPayCardInfoExample;
import com.zx.map.service.HistoryPayCardInfoService;
import com.zx.map.service.model.HistoryPayCardInfoModel;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("historyPayCardInfoService")
public class HistoryPayCardInfoServiceImpl implements HistoryPayCardInfoService {

	@Autowired
	private HistoryPayCardInfoDao dao;
                                            	/**
	 * 根据Model查找列表
	 */
	public List<HistoryPayCardInfoModel> getListByModel(HistoryPayCardInfoModel model) throws DataAccessException {
		HistoryPayCardInfoExample example = new HistoryPayCardInfoExample();
		HistoryPayCardInfoExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(model.getMerchantId()))
        {
             criteria.andMerchantIdEqualTo(model.getMerchantId());
        }
        if(StringUtils.isNotBlank(model.getBankAccount()))
        {
             criteria.andBankAccountEqualTo(model.getBankAccount());
        }
		List<HistoryPayCardInfo> list = dao.getListByExample(example);
		List<HistoryPayCardInfoModel> modelList = new ArrayList<HistoryPayCardInfoModel>();
		for (HistoryPayCardInfo o : list) {
			HistoryPayCardInfoModel temp = new HistoryPayCardInfoModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;
	}
	
	/**
	 * 新增或修改
	 */
	public void addOrUpdate(HistoryPayCardInfoModel model) throws DataAccessException {
		List<HistoryPayCardInfoModel> list = this.getListByModel(model);
		if(null != list && list.size() ==1)
		{
			model.setPaycardId(list.get(0).getPaycardId());
			this.update(model);
		}else{
			this.add(model);
		}
	}

	/**
	 * 新增
	 */
	public String add(HistoryPayCardInfoModel model) throws DataAccessException {
		HistoryPayCardInfo temp = new HistoryPayCardInfo();
		BeanUtils.copyProperties(model, temp);
		String key = dao.add(temp);
		return key;
	}

	/**
	 * 修改
	 */
	public String update(HistoryPayCardInfoModel model) throws DataAccessException {
		HistoryPayCardInfo temp = new HistoryPayCardInfo();
		BeanUtils.copyProperties(model, temp);
		dao.update(temp);
		return model.getPaycardId().toString();
	}

	/**
	 * 删除
	 */
	public void delete(HistoryPayCardInfoModel model) throws DataAccessException {
		HistoryPayCardInfo temp = new HistoryPayCardInfo();
		BeanUtils.copyProperties(model, temp);
		dao.delete(temp);
	}
}
